package com.igeek.yygh.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.yygh.model.healthy.Healthy;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author author
 * @since 2024-08-22
 */
public interface HealthyMapper extends BaseMapper<Healthy> {
    @Select("SELECT healthy.id,older_id,weight,heart,blood,sugar,name,create_time,o.sex " +
            "FROM healthy  " +
            "JOIN elders o ON healthy.older_id = o.id " +
            "WHERE healthy.create_time = ( " +
            "    SELECT MAX(h2.create_time) " +
            "    FROM healthy h2 " +
            "    WHERE h2.older_id = healthy.older_id " +
            ") "+
            "AND o.name LIKE CONCAT('%', COALESCE(#{name}, ''), '%') " )
    public List<Healthy> getlist(Healthy healthy);


    @Select("<script>" +
            "select h.*, o.name from  healthy h join elders o ON h.older_id = o.id" +
            "<where>" +
            " <if test='olderId != null and olderId!= \"\"'>  " +
            "     and h.older_id = #{olderId}  " +
            "    </if> " +
            " <if test='name != null and name!= \"\"'>  " +
            "     and o.name LIKE CONCAT('%',#{name},'%')  " +
            "    </if> " +
            " <if test='startTime != null and startTime!= \"\"'>  " +
            " <if test='endTime != null and endTime!= \"\"'>  " +
            "     and h.create_time between #{startTime} and #{endTime}  " +
            "    </if> " +
            "    </if> " +
            "</where>" +
            "</script>")
    IPage<Healthy> selectUserWithDepartment(Page<Healthy> page, Integer olderId, String name, String startTime, String endTime);
}
